/*
 * Created on 4/Abr/2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package org.cyborgas.xmltool;

import org.dom4j.Attribute;
import org.dom4j.Comment;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

/**
 * @author rino
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class XmlLabelProvider extends LabelProvider {

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
	 */
	public String getText(Object item) {
		String result = null;

		if (item instanceof Node) {
			result = ((Node) item).getName();

			if (item instanceof Attribute) {
				String value = ((Attribute) item).getValue();
				if (value.length() > 0) {
					result += ": " + value;
				}
			} else if (item instanceof Element) {
				Element element = (Element) item;
				if (!element.getNamespacePrefix().equals("")) {
					result = element.getNamespacePrefix() + ":" + result;
				}
				String value = ((Element) item).getTextTrim();
				if (value.length() > 0) {
					result += ": " + value;
				}
			} else if (item instanceof Comment) {
				result = "(Comment)";
			} else if (item instanceof ProcessingInstruction) {
//				ProcessingInstruction element = (ProcessingInstruction) item;
//				result = element.getName();
			} else {
				result = item.toString();
			}
		} else {
			result = item.toString();
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
	 */
	public Image getImage(Object element) {
		Image result = null;

		if (element instanceof Attribute) {
			result = Util.getImageResource("attribute");
		} else if (element instanceof Element) {
			result = Util.getImageResource("element");
		} else if (element instanceof Comment) {
			result = Util.getImageResource("comment");
		} else if (element instanceof ProcessingInstruction) {
			result = Util.getImageResource("pi");
		}
		
		return result;
	}

}
